package com.kongwg.java;


import org.junit.Test;

import java.util.*;

/**
 * @author kongwg
 * @create 2020-12-15 11:38
 */
public class MapTest {

    @Test
    public void test2(){
        Map map = new HashMap();
        map = new LinkedHashMap();
        map.put(123,"AA");
        map.put(456,"BB");
        map.put(789,"CC");
        System.out.println(map);
    }

    @Test
    public void test(){
        Map map = new HashMap();
        map.put(null,null);

//        Map map1 = new Hashtable();
//        map1.put(null,null);


    }


    @Test
    public void test3(){
        //map方法中的方法
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",123);
        map.put("AA",83);


        System.out.println(map);

        Map map1 = new HashMap();

        map1.put("CC",123);
        map1.put("DD",123);

        map.putAll(map1);

        System.out.println(map);
        //remove()
        Object value = map.remove("CC");
        System.out.println(value);
        System.out.println(map);

        //clear()
        map.clear();
        System.out.println(map);
    }

    @Test
    public void test4(){
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",123);
        map.put("AA",83);

        Map map1 = new HashMap();
        map1.put("AA",123);
        map1.put(45,123);
        map1.put("BB",123);
        map1.put("AA",83);

        //get();
        Object value = map.get("BB");
        System.out.println(value);
        //cotainsKey()
        boolean isExist = map.containsKey("BB");
        System.out.println(isExist);

        boolean b = map.containsValue(123);
        System.out.println(b);


        System.out.println(map.size());


        boolean empty = map.isEmpty();
        System.out.println(empty);

        boolean equals = map.equals(map1);
        System.out.println(equals);

    }

    @Test
    public void test5(){
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",123);
        map.put("AA",83);

        //keySet()
        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }


        //values
        Collection values = map.values();
        for (Object obj : values){
            System.out.println(obj);
        }


        System.out.println("**********************");
        //EntrySet()


        Set entrySet = map.entrySet();
        Iterator iterator1 = entrySet.iterator();
        while (iterator1.hasNext()){
//            System.out.println(iterator.next());

            Object obj = iterator1.next();
            Map.Entry entry = (Map.Entry) obj;

            System.out.println(entry.getKey()+ "-->" + entry.getValue());
        }

            //方式二
        Set keySet1 = map.keySet();
        Iterator iterator2 = keySet1.iterator();
        while (iterator2.hasNext()){
            Object key = iterator2.next();
            Object value = map.get(key);
            System.out.println(key + "-->" + value);

        }
    }


}
